﻿/* API函数：Window类型相关
 */

namespace LSLib.API
{
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;

    [Serializable]
    public static class Window
    {
        [DllImport("user32.dll")]
        internal static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

        [DllImport("user32.dll")]
        internal static extern IntPtr ChildWindowFromPoint(IntPtr hWndParent, Point Point);

        [DllImport("user32.dll", SetLastError = true)]
        internal static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        internal static extern IntPtr GetParent(IntPtr hWnd);

        #region AnimateWindow
        public enum AnimateWindowFlags : uint
        {
            AW_HOR_POSITIVE = 0x00000001,//自左向右显示
            AW_HOR_NEGATIVE = 0x00000002,//自右向左显示
            AW_VER_POSITIVE = 0x00000004,//自顶向下显示
            AW_VER_NEGATIVE = 0x00000008,//自底向上显示
            //若使用了AW_HIDE标志，则使窗口向内重叠；
            //若未使用AW_HIDE标志，则使窗口向外扩展。
            AW_CENTER = 0x00000010,     //中心
            AW_HIDE = 0x00010000,       //隐藏窗口
            AW_ACTIVATE = 0x00020000,   //激活窗口
            AW_SLIDE = 0x00040000,      //滑动效果
            AW_BLEND = 0x00080000,      //淡出效果
        }

        [DllImport("user32")]
        internal static extern bool AnimateWindow(IntPtr hwnd, int time, AnimateWindowFlags flags);
        #endregion

        #region Cursor相关
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        internal static extern IntPtr LoadCursorFromFile(string lpFileName);

        [DllImport("user32.dll")]
        internal static extern bool SetSystemCursor(IntPtr hcur, uint id);

        public enum OCR_SYSTEM_CURSORS : uint
        {
            /// <summary>Standard arrow and small hourglass</summary>
            OCR_APPSTARTING = 32650,
            /// <summary>Standard arrow</summary>
            OCR_NORMAL = 32512,
            /// <summary>Crosshair</summary>
            OCR_CROSS = 32515,
            /// <summary>Windows 2000/XP: Hand</summary>
            OCR_HAND = 32649,
            /// <summary>Arrow and question mark</summary>
            OCR_HELP = 32651,
            /// <summary>I-beam</summary>
            OCR_IBEAM = 32513,
            /// <summary>Slashed circle</summary>
            OCR_NO = 32648,
            /// <summary>Four-pointed arrow pointing north, south, east, and west</summary>
            OCR_SIZEALL = 32646,
            /// <summary>Double-pointed arrow pointing northeast and southwest</summary>
            OCR_SIZENESW = 32643,
            /// <summary>Double-pointed arrow pointing north and south</summary>
            OCR_SIZENS = 32645,
            /// <summary>Double-pointed arrow pointing northwest and southeast</summary>
            OCR_SIZENWSE = 32642,
            /// <summary>Double-pointed arrow pointing west and east</summary>
            OCR_SIZEWE = 32644,
            /// <summary>Vertical arrow</summary>
            OCR_UP = 32516,
            /// <summary>Hourglass</summary>
            OCR_WAIT = 32514
        }
        #endregion

        #region SetClassLong
        public static IntPtr SetClassLongPtr(HandleRef hWnd, int nIndex, IntPtr dwNewLong)
        {
            if (IntPtr.Size > 4)
                return SetClassLongPtr64(hWnd, nIndex, dwNewLong);
            else
                return new IntPtr(SetClassLongPtr32(hWnd, nIndex, unchecked((uint)dwNewLong.ToInt32())));
        }

        [DllImport("user32.dll", EntryPoint = "SetClassLong")]
        internal static extern uint SetClassLongPtr32(HandleRef hWnd, int nIndex, uint dwNewLong);

        [DllImport("user32.dll", EntryPoint = "SetClassLongPtr")]
        internal static extern IntPtr SetClassLongPtr64(HandleRef hWnd, int nIndex, IntPtr dwNewLong);

        public enum ClassLongFlags : int
        {
            GCLP_MENUNAME = -8,
            GCLP_HBRBACKGROUND = -10,
            GCLP_HCURSOR = -12,
            GCLP_HICON = -14,
            GCLP_HMODULE = -16,
            GCL_CBWNDEXTRA = -18,
            GCL_CBCLSEXTRA = -20,
            GCLP_WNDPROC = -24,
            GCL_STYLE = -26,
            GCLP_HICONSM = -34,
            GCW_ATOM = -32
        }
        #endregion

        #region GetClassLong
        public static IntPtr GetClassLongPtr(HandleRef hWnd, int nIndex)
        {
            if (IntPtr.Size > 4)
                return GetClassLongPtr64(hWnd, nIndex);
            else
                return new IntPtr(GetClassLongPtr32(hWnd, nIndex));
        }

        [DllImport("user32.dll", EntryPoint = "GetClassLong")]
        internal static extern uint GetClassLongPtr32(HandleRef hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "GetClassLongPtr")]
        internal static extern IntPtr GetClassLongPtr64(HandleRef hWnd, int nIndex);
        #endregion
    }
}
